import java.util.ArrayList;

/**
 * This class stores information about the executed moves and allows to undo a move.
 * @author mx109
 *
 */
public class History {
	class Move{
		int playerId;
		boolean boxMove;
		Direction dir;
	}
	
	private Board board;
	
	private ArrayList<Move> T=new ArrayList<Move>();
	
	/**
	 * Recommended constructor
	 * @param b Board on which information is to be stored
	 */
	public History(Board b){
		board=b;
	}
	
	/**
	 * Add information to history
	 * @param playerId ID of the player who made ​​a move 
	 * @param dir   move direction
	 * @param boxMove Information about whether the box was moved (in this move)
	 */
	public void add(int playerId,Direction dir, boolean boxMove){
		
		Move mov = new Move();
		mov.playerId=playerId;
		mov.dir=dir;
		mov.boxMove=boxMove;
		T.add(mov);
		
		
	}
	
	/**
	 * Revoke the latest move
	 * @return true if there was move in history
	 */
	public boolean back(){
		if (T.size()==0) return false;
		Move mov=T.get(T.size()-1);
		T.remove(T.size()-1);
		switch(mov.dir){
			case UP: mov.dir=Direction.DOWN; break;
			case DOWN: mov.dir=Direction.UP; break;
			case LEFT: mov.dir=Direction.RIGHT; break;
			case RIGHT: mov.dir=Direction.LEFT; break;
		}
		Player pl=board.getPlayer(mov.playerId);
		Position pos=new Position(pl.getPosition());
		switch(mov.dir){
			case UP: pos.y++; break;
			case DOWN:pos.y--; break;
			case LEFT: pos.x++; break;
			case RIGHT: pos.x--; break;
		}
		board.getPlayer(mov.playerId).moveBack(mov.dir,mov.boxMove);//to nie działa
		if (pos.x>=board.rozx || pos.x<0 || pos.y>=board.rozy || pos.y<0) return true;
		Field fil=board.check(pos);
		
		if (fil.movObj instanceof Box && mov.boxMove==true) 
			fil.movObj.move(mov.dir);
		return true;
	}
}
